package cn.uin.domain.auth.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @Auther: uIn
 * @Date: 2025/10/19 - 10 - 19 - 16:15
 * @Description: cn.uin.domain.auth.model.entity
 * @version: 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoDetailEntity implements UserDetails {

    /** 用户名 */
    private String username;

    /** 密码 */
    private String password;

    /** 手机号 */
    private String phone;

    /** 邮箱 */
    private String email;

    /** 状态(0:启用,1:禁用) */
    private Integer status;

    /** 角色 */
    private List<HomeRoleEntity> roles = new ArrayList<>();

    /** 菜单 */
    private List<HomeMenuEntity> menus = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menus)) {
            for (HomeMenuEntity authority : menus) {
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(authority.getMenuCode());
                authorities.add(auth);
            }
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return status.equals(0);
    }

    @Override
    public boolean isAccountNonLocked() {
        return status.equals(0);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return status.equals(0);
    }
}
